package com.you.learning.rocketmq.consumer;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * @Description 从RocketMQ消费消息
 * @Author yough
 * @Version V1.0.0
 * @Since 1.0
 * @Date 2021/3/23
 */
public class ConsumerService {
    public static void main(String[] args) throws Exception {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("test_consumer");
        consumer.setNamesrvAddr("112.124.46.106:9876");
        consumer.subscribe("test_topic_1", "");
        // 设置每次消费几条数据
        consumer.setConsumeMessageBatchMaxSize(5);
        // 一次拉取多少条数据
        consumer.setPullBatchSize(5);
        // 注册一个回调接口
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
                                                            ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                System.out.println("--------------------------");
                System.out.println(msgs);
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        consumer.start();
    }
}
